/**
 * Project Name:umale-maintenance
 * File Name:JdbcDao
 * Package Name:test.spring.data.dao.impl
 * Date: 2018/11/20 14:17
 * Copyright (c) 2018, CCI All Rights Reserved.
 */
package test.spring.data.dao.impl;

import my.spring.ioc.annotation.Bean;
import my.spring.transaction.TransactionResourceManager;
import test.spring.data.dao.Dao;
import test.spring.ioc.NewsBean;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author fengbo
 * @version 1.0.0
 * @date 2018/11/20 14:17
 * @since JDK 1.8
 */
@Bean("dao")
public class JdbcDao implements Dao {

    @Override
    public void insert(NewsBean newsBean) {
        Connection conn = (Connection) TransactionResourceManager.getResource();
        try {
            Statement statement = conn.createStatement();
            String sql = "INSERT INTO `seckill` ( " +
                    " `name`, " +
                    " `end_time`, " +
                    " `start_time`, " +
                    " `create_time`, " +
                    " `update_time` " +
                    " ) " +
                    " VALUES " +
                    " ( '" + newsBean.getName()
                    + "', '" + newsBean.getBirthday()
                    + "', now(), now(), now()"
                    + " ) ";
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            // 应该抛一个Spring自定义异常
            throw new RuntimeException(e);
        }
    }
}
